package com.jieao.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jieao.common.core.domain.Ztree;
import com.jieao.common.core.text.Convert;
import com.jieao.common.utils.StringUtils;
import com.jieao.common.utils.date.DateUtil;
import com.jieao.content.domain.Diary;
import com.jieao.content.domain.DiarySub;
import com.jieao.content.dto.DiaryDto;
import com.jieao.content.mapper.DiaryMapper;
import com.jieao.content.mapper.DiarySubMapper;
import com.jieao.content.service.IDiaryService;
import com.jieao.framework.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description
 * @Author weifanl
 * @Date2020/5/21 15:33
 **/
@Service
public class DiaryServiceImpl extends ServiceImpl<DiaryMapper, Diary> implements IDiaryService {
    @Autowired
    private DiarySubMapper diarySubMapper;

    @Override
    public List<DiaryDto> selectDiaryList(DiaryDto diary) {
        return baseMapper.selectDiaryList(diary);
    }

    @Override
    public List<DiaryDto> selectFrontDiaryList(DiaryDto diary) {
        String timeOption = diary.getTimeOption();
        //查询时间
        if (StringUtils.isNotEmpty(timeOption)) {
            String year = timeOption.substring(0, 4);
            String month = timeOption.substring(4, 6);

            String lastDayOfMonth = DateUtil.getLastDayOfMonth(Integer.parseInt(year), Integer.parseInt(month));

            Map<String, Object> params = new HashMap<>();
            params.put("beginTime",year + "-" + month + "-" + "01");
            params.put("endTime",lastDayOfMonth);

            diary.setParams(params);
        }

        List<DiaryDto> diaryDtos = baseMapper.selectFrontDiaryList(diary);
        for (DiaryDto diaryDto : diaryDtos) {
            String content = diaryDto.getContent();
            while (content.indexOf("![](") >= 0) {
                int imgStart = content.indexOf("![](");
                content = content.replace(content.substring(imgStart, content.indexOf(")", imgStart) + 1), "");
            }
            Integer end = (content.length() - 1 > 2000 ? 2000 : content.length() - 1);
            content = content.substring(0, end) + "...";
            diaryDto.setContent(content);
        }
        return diaryDtos;
    }


    @Transactional
    @Override
    public int insertDiary(DiaryDto diary) {
        //插入diary_sub
        DiarySub diarySub = diary.createDiarySub();
        diarySub.setCreateBy(ShiroUtils.getLoginName());
        diarySub.setCreateTime(new Date());
        diarySubMapper.insert(diarySub);

        //插入diary
        diary.setSubId(diarySub.getId());
        return baseMapper.insert(diary);
    }

    @Override
    public DiaryDto selectDiaryById(Long diaryId) {
        DiaryDto diary = baseMapper.selectDiaryById(diaryId, ShiroUtils.getUserId());
        return diary;
    }

    @Transactional
    @Override
    public int updateDiary(DiaryDto catalog) {

        DiarySub diarySub = catalog.createDiarySub();
        diarySub.setId(catalog.getSubId());
        diarySubMapper.updateContentById(diarySub);

        Diary diary = new Diary();
        BeanUtils.copyProperties(catalog, diary);
        int i = baseMapper.updateById(diary);
        return i;
    }

    @Override
    public int deleteDiaryByIds(String ids) {
        int i = 0;
        Long[] diaryIds = Convert.toLongArray(ids);
        for (Long diaryId : diaryIds) {
            baseMapper.deleteDiaryById(diaryId);
            ++i;
        }

        return i;
    }

    @Override
    public int change(DiaryDto catalog) {
        return baseMapper.change(catalog);
    }


    /**
     * 查询目录管理树
     *
     * @param diary 目录信息
     * @return 所有目录信息
     */
    @Override
    public List<Ztree> selectDiaryTree(DiaryDto diary) {
        List<DiaryDto> diaryList = baseMapper.selectDiaryTreeList(diary);
        List<Ztree> ztrees = initZtree(diaryList);
        return ztrees;
    }

    @Override
    public List<Map<String, Object>> getTimeData(Long id) {
        return baseMapper.getTimeData(id);
    }

    /**
     * 对象转目录树
     *
     * @param diaryList 文章列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<DiaryDto> diaryList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (DiaryDto diary : diaryList) {
            Ztree ztree = new Ztree();
            ztree.setId(diary.getId());
            ztree.setpId(diary.getParentId());
            ztree.setName(diary.getTitle());
            ztree.setTitle(diary.getTitle());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
